﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using wp7cinema.View.Twitter;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using wp7cinema.Common;
using GalaSoft.MvvmLight.Command;

namespace wp7cinema.ViewModel
{
    public struct TwitterItem
    {
        public string UserName { get; set; }
        public string Message { get; set; }
        public string ImageSource { get; set; }
    }
    public class TwitterViewModel : ViewModelBase
    {
        TwitterAccess _twitterSettings;

        private IEnumerable<TwitterItem> _twittItems;
        public IEnumerable<TwitterItem> TwittItems
        {
            get { return _twittItems; }
            set
            {
                _twittItems = value;
                RaisePropertyChanged("TwittItems");
            }
        }

        public RelayCommand<string> NewTweetCommand { get; set; }
        public void NewTweetCommandExecute(string message)
        {
            var twitter = new TwitterHelper(_twitterSettings);
            twitter.NewTweet(message);

            twitter.TweetCompletedEvent += (sender, e) =>
            {
                GetTweet();
            };
        }
        public RelayCommand<string> GetTweetCommand { get; set; }
        public void GetTweetCommandExecute(string message)
        {
            _twitterSettings = Helper.LoadSetting<TwitterAccess>(TwitterHelper.TwitterAccess);
            if (_twitterSettings == null) return;

            GetTweet();
        }

        public TwitterViewModel()
        {
            if (IsInDesignMode)
            {
                TwittItems = new ObservableCollection<TwitterItem> {
                    new TwitterItem { UserName = "오주노", Message="트윗 테스틍", ImageSource="http://a0.twimg.com/profile_images/1116236368/joono_normal.jpg" },
                    new TwitterItem { UserName = "오주노", Message="트윗 테스틍", ImageSource="http://a0.twimg.com/profile_images/1116236368/joono_normal.jpg" },
                    new TwitterItem { UserName = "오주노", Message="트윗 테스틍", ImageSource="http://a0.twimg.com/profile_images/1116236368/joono_normal.jpg" },
                    new TwitterItem { UserName = "오주노", Message="트윗 테스틍", ImageSource="http://a0.twimg.com/profile_images/1116236368/joono_normal.jpg" },
                    new TwitterItem { UserName = "오주노", Message="트윗 테스틍", ImageSource="http://a0.twimg.com/profile_images/1116236368/joono_normal.jpg" },
                    new TwitterItem { UserName = "오주노", Message="트윗 테스틍", ImageSource="http://a0.twimg.com/profile_images/1116236368/joono_normal.jpg" }
                };
            }
            else
            {
                NewTweetCommand = new RelayCommand<string>(NewTweetCommandExecute);
                GetTweetCommand = new RelayCommand<string>(GetTweetCommandExecute);
            }
        }

        private void GetTweet()
        {
            string url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + _twitterSettings.ScreenName;
            WebClient client = new WebClient();
            client.DownloadStringCompleted += TweetDownloaded;
            client.DownloadStringAsync(new Uri(url));
        }

        void TweetDownloaded(object sender, DownloadStringCompletedEventArgs e)
        {
            string xml = e.Result;
            XDocument xdoc = XDocument.Parse(xml);

            TwittItems = xdoc.Descendants("status")
                .Select(status => new TwitterItem
                {
                    ImageSource = status.Element("user").Element("profile_image_url").Value,
                    UserName = status.Element("user").Element("name").Value,
                    Message = status.Element("text").Value
                });
        }

        private void PostTweet(string tweet)
        {
            var twitter = new TwitterHelper(_twitterSettings);
            twitter.NewTweet(tweet);

            //if (String.IsNullOrEmpty(TweetTextBox.Text))
            //    return;

            //ProgressBar.Visibility = Visibility.Visible;
            //ProgressBar.IsIndeterminate = true;

            //var twitter = new TwitterHelper(_twitterSettings);

            //// Successful event handler, navigate back if successful
            //twitter.TweetCompletedEvent += (sender, e) =>
            //{
            //    ProgressBar.Visibility = Visibility.Collapsed;
            //    ProgressBar.IsIndeterminate = false;
            //};

            //// Failed event handler, show error
            //twitter.ErrorEvent += (sender, e) =>
            //{
            //    ProgressBar.Visibility = Visibility.Collapsed;
            //    ProgressBar.IsIndeterminate = false;
            //    MessageBox.Show("There was an error connecting to Twitter");
            //};

            //twitter.NewTweet(TweetTextBox.Text);
        }
    }
}
